<template>
	<view class="wrap bg-white">
		<js-error mode="bottom" ref="jsError" @update:errorClose="onErrorClose"></js-error>
		<u-sticky class="u-sticky">
			<view class="cu-bar bg-white ">
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<u-input @confirm="search" :focus="firstFocus"  v-model="cbarCode" type="text" placeholder="请扫描识别码"></u-input>
				</view>
				<view class="action" @click="toScan">
					<text class="cuIcon-scan" style="font-size: 56rpx;"></text>
				</view>
			</view>
		</u-sticky>
		<u-form class="form" :model="model" ref="uForm" label-position="left" v-if="show">
			<u-form-item label="父层箱码:" prop="parentSnCode" label-width="140" >
				<u-input v-model="model.parentSnCode" :disabled="true" placeholder='' type="text" di maxlength="64"></u-input>
				<template #public>
					<view class="flex align-center justify-end margin-top">
						<!-- <view class="cuIcon-scan text-sl" @click="search"></view> -->
						<button @click="bindSnCode" class="cu-btn round line-blue shadow">绑定子层</button>
					</view>
				</template>
			</u-form-item>
			<!-- -->
			<view v-if="boxList.length>0" >
				<view class="action bg-white"
					style="font-size: 20px;align-items: center;display: flex;padding: 20rpx;">
					<u-icon name="/static/image/detail.png" size="80"></u-icon>
					<text class="cuIcon-titles text-bold">子层编码信息</text>	
					<view style="flex: 1;text-align: right;" >
						<button class="cu-btn round line-blue shadow" @tap="unSubmit()">
							<!-- 发货 -->
							 解绑
						</button>
					</view>	
				</view>
				<view>
					<view v-for="(item,index) in boxList" :key="index" class="cu-item shadow " >
						<view class="cu-form-group" style="display: flex;">
							<view style="display: inline-block;padding: 5px;background: #3E97B0;color: #fff;min-width: 40px;text-align: center;font-style: italic;font-weight: bold">
								{{ boxList.length -index }}
							</view>
							<view>{{ item.snCode }}</view>
							<!-- <view>
								<text style="font-size: 35px;" class="cuIcon-deletefill text-sl text-red" @tap="delsoOutDetail(item,index)"></text>
							</view> -->
						</view>
					</view>
				</view>
			</view>
		</u-form>
		
		<movable-area class="movable-area1">
			<movable-view class="movable-view" :x="x" :y="y" direction="all">
				<view @click="backInv" class="custom-style" style="width: 90px; height: 70px; color: #fff; border-radius: 10rpx;">
					<view style="font-size: 16px;display: flex; flex-direction: column;text-align: center;">
						<text class="cuIcon-camera text-xxls text-blue"></text>
						<view class="text-blue">条码识别</view>
					</view>
				</view>
			</movable-view>
		</movable-area>
		<!-- <movable-area class="movable-area1">
			<movable-view class="movable-view" :x="x" :y="y1" direction="all">
				<view @click="reset" class="custom-style" style="width: 90px; height: 70px; color: #fff; border-radius: 10rpx;">
					<view style="font-size: 16px;display: flex; flex-direction: column;text-align: center;">
						<text class="cuIcon-refresh text-xxls text-blue"></text>
						<view class="text-blue">重置</view>
					</view>
				</view>
			</movable-view>
		</movable-area> -->
		<movable-area class="movable-area2">
			<movable-view class="movable-view" :x="x1" :y="y1" direction="all">
				<view @click="reset" class="custom-style" style="width: 90px; height: 70px; color: #fff; border-radius: 10rpx;">
					<view style="font-size: 16px;display: flex; flex-direction: column;text-align: center;">
						<text class="cuIcon-refresh text-xxls text-blue"></text>
						<view class="text-blue">重置</view>
					</view>
				</view>
			</movable-view>
		</movable-area>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				size:-1,
				show:false,
				cbarCode:'',
				model:{
					parentSnCode:'',
					procode:'',
					cinvname:'',
					bachNum:'',
					comounit:'',
					cCode:'',
					cDLCode:'',
					cCusName:'',
					scDate:'',
					cEBExpressCode:'',
					cExpressCoName:'',

				},
				cacheData:{
					data:[],
					parentSnCode:''
				},
				isParentSnCode:false,
				boxList:[],
				firstFocus: true,
				x: 540, //x坐标
				y: 540, //y坐标
				x1: 610,
				y1: 610
			}
		},
		onLoad() {
			var _this = this;
			setTimeout(() => {
				this.firstFocus = true
			},0)
		},
		methods: {
			reset(){
				this.model = { }
				this.boxList = [ ]
				this.show = false
				this.isParentSnCode = false
				this.cbarCode = ''
				console.log('cbarCode',this.cbarCode)
				this.handleFocus()
			},
			unSubmit(){
					// unbindList
				var _this = this;
				uni.navigateTo({
					url: '/pages/zgz/bind/unBindList',
					events: {
						Filter(data) {
							console.log(data,'data')
							_this.boxList = data.data;
							_this.model.parentSnCode = data.parentSnCode;						
						}
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						//res.eventChannel.emit('initfilter', that.queryData)
						_this.cacheData = {
							data:_this.boxList,
							parentSnCode:_this.model.parentSnCode,
						}
						console.log('cacheData',_this.cacheData)
						res.eventChannel.emit('initfilter', _this.cacheData);
					}
				})
			},
			/** 扫码查询*/
			search(){
				this.firstFocus = false
				var code = this.cbarCode;
				if(code == undefined || code == ''){
					//闪现提示
					// this.$u.toast('您扫描的条码有误,请重新扫码。');
					this.handleFocus()
					//确认提示
				}else{
					if(code.indexOf("http://") > -1 || code.indexOf("https://") > -1){
						code = code.split("=")[1];
						console.log('code---',code)
						this.loadData(code);
					}else{
						this.loadData(code);
					}
				}
			},
			/** 扫码 */
			toScan() {
				// #ifdef APP-PLUS
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module");
				mpaasScanModule.mpaasScan({
					// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
					'scanType': ['qrCode', 'barCode'],
					// 是否隐藏相册，默认false不隐藏
					'hideAlbum': false,
					//ios需要设置这个参数，只支持中英文 zh-Hans、en，默认中文                       
					'language': 'en',
					//相册选择照片识别错误提示(ios)
					'failedMsg': '未识别到二维码，请重试',
					//Android支持全屏需要设置此参数
					'screenType': 'full'
				},
					async (ret) => {
						console.log('ret', ret)
						if (ret.resp_message == 'success') {							
							var code = ret.resp_result;
							if(code.indexOf("http://") > -1  || code.indexOf("https://") > -1){
								code = code.split("=")[1];
								this.loadData(code);
							}else{
								this.loadData(code);
							}
						}
					})
				// #endif
				// #ifdef H5 || MP-WEIXIN || MP-QQ 
				uni.scanCode({ 
					onlyFromCamera: true,
					success: function(res) {
						var code = res.result;
						
						if(code.indexOf("http://") > -1  || code.indexOf("https://") > -1){
							code = code.split("=")[1];
							this.loadData(code);
						}else{
							this.loadData(code);
						}
					}.bind(this)
				});
				// #endif
			},
			/** 数据加载 */
			async loadData(code){
				console.log(code,'code',this.model.parentSnCode)
				//105343222202010000057143
				// var code = '00202204500929799129'
				if(code != undefined && code != ''){
					// var query = {
					// 	"barcode":code
					// }
					if(this.isParentSnCode){
						var query = {
							"parentSnCode":this.model.parentSnCode,
							"snCode":code
						}
						
						console.log('boxList',this.boxList)
						await this.$u.api.zgz.wmsBarBind(query).then(res=>{
							this.cbarCode = "";
							if(res.result != 'true'){
								this.makeSound("sb");
								// this.$u.toast(res.message);
								this.$refs.jsError.showError('', res.message, 'error');
							}else{
								this.makeSound("cg");
								setTimeout(()=>{
									this.cbarCode = ''
								})
								this.boxList.unshift({
									snCode:code
								})
								if(this.vuex_config.deviceType === this.vuex_config.deviceTypePDA){
									this.handleFocus()
								}
							}
							
						})
					}else{
						this.model.parentSnCode = code
						await this.$u.api.zgz.getBindInfo({
							"parentSnCode":code
						}).then(res=>{
							if(res.result != 'true'){
								this.makeSound("sb");
								// this.$u.toast(res.message);
								this.$refs.jsError.showError('', res.message, 'error');
							}else{
								this.makeSound("cg");
								setTimeout(()=>{
									this.cbarCode = ''
								})
								this.boxList = res.data
								// this.boxList 倒序
							}
							console.log('cbarCode',this.cbarCode)
							
						})
						console.log('cbarCode',this.cbarCode)
						setTimeout(()=>{
							this.cbarCode = ''
						})
						if(this.vuex_config.deviceType === this.vuex_config.deviceTypePDA){
							this.handleFocus()
						}	
					}
					
				}
				this.show = true
				setTimeout(()=>{
					this.cbarCode = ''
				})
			},
			bindSnCode(){
				this.isParentSnCode = true
				// this.backInv()
				if(this.vuex_config.deviceType === this.vuex_config.deviceTypePDA){
					this.handleFocus()
				}
			},
			backInv(){
				if(this.vuex_config.deviceType === this.vuex_config.deviceTypeAPP){
					this.search()
				}else if(this.vuex_config.deviceType === this.vuex_config.deviceTypePDA){
					this.handleFocus()
				}
			},
			handleFocus() {
				var _that = this;
				_that.firstFocus = false;
				setTimeout(() => {
					_that.firstFocus = true;
				}, 500);
			},
			onErrorClose(result) {
				console.log('result',result)
			//   获取focus 焦点
				this.firstFocus = result;
				setTimeout(() => {
					this.barCode = ''
					if(this.vuex_config.deviceType === this.vuex_config.deviceTypePDA){
						this.firstFocus = true;
					}
					
				}, 100);
			 
			},
			makeSound(name) {
				console.log("=====testClick=====");
				let src = '/static/jeesite/' + name + '.mp3';
				//实例化声音  
				const Audio = uni.createInnerAudioContext();
				Audio.autoplay = true;
				Audio.src = src; //音频地址  
				Audio.play(); //执行播放  
				Audio.onError((res) => {
				});
				Audio.onPause(function () {
					console.log('end');
					Audio.destroy();
				});
			},
		}
	}
</script>

<style>
@import '../btdm.css';
</style>
<style lang="scss" scoped>
	$all_width: 96rpx;
	$all_height: 96rpx;
	.movable-area1 {
		z-index: 888;
		height: 97vh;
		width: 650rpx;
		position: fixed;
		right: $all_width;
		top: -($all_height + 100rpx);
		pointer-events: none; //此处要加，鼠标事件可以渗透
		.movable-view {
			width: $all_width;
			height: $all_height;
			pointer-events: auto; //恢复鼠标事件
			image {
				width: $all_width;
				height: $all_height;
			}
		}
	}
	.movable-area2 {
		z-index: 888;
		height: 97vh;
		width: 650rpx;
		position: fixed;
		right: -40rpx;
		top: -$all_height;
		pointer-events: none; //此处要加，鼠标事件可以渗透
		.movable-view {
			width: 230rpx;
			// height: $all_height;
			pointer-events: auto; //恢复鼠标事件
		}
	}
	.text-xxls{
		font-size:60rpx;
	}
	.u-form-item{
		font-size: 30rpx !important;
	}
	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		background: #fff;z-index: 999;
		border-top: 1px solid #eee;
	}
	.cu-btn{
		padding: 0 60rpx !important;
	}
	.cu-btns{
		padding: 0 80rpx !important;
	}
	.button {
		font-size: 32rpx;
		color: #666666;
		line-height: 40rpx;
		padding: 12rpx 40rpx;
		margin-bottom: 20rpx;
		background: #e2e2e2;
		border-radius: 180rpx;
	}
	
	.button:hover {
		background: #3e97b0;
		color: #ffffff;
	}
	
</style>
